package com.wiz.web.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.gson.reflect.TypeToken;
import com.wiz.common.Constant;
import com.wiz.web.manager.HttpManager;
import com.wiz.web.service.AccountService;
import com.wiz.web.service.HqStationService;
import com.wiz.web.service.PayService;
import com.wiz.web.vo.criteria.PayCriteria;
import com.wiz.web.vo.criteria.WxPayCriteria;

@Service("payService")
public class PayServiceImpl extends BasicServiceImpl implements PayService,Constant {
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	@Resource
	private AccountService accountService;
	@Resource
	private HqStationService hqStationService;
	@Resource
	private HttpManager httpManager;
	

	@Override
	public String barPay(String order, String hqId, String stationId,int payChannel) throws Exception{//三方支付
		WxPayCriteria criteria = gson.fromJson(order, new TypeToken<WxPayCriteria>(){}.getType());
		criteria.setUseType(1);
		List<NameValuePair> formparams=new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("order", gson.toJson(criteria)));
		formparams.add(new BasicNameValuePair("hqId", hqId));
		formparams.add(new BasicNameValuePair("stationId", stationId));
		formparams.add(new BasicNameValuePair("payChannel", String.valueOf(payChannel)));
		String result = httpManager.getHttpResponseByPost(CLOUD_REMOTE_PAY, PAY_WX_BAR, formparams);
		return result;
	}


	@Override
	public String balancePay(PayCriteria criteria) throws Exception {
		if(StringUtils.isBlank(criteria.getMemberId())){
			log.warn(criteria.getRefId()+" balancePay miss param MEMBER_ID");
			return RESPONSE_CODE_PARAM_ERROR;
		}
		return accountService.balancePay(criteria.getAmountType(), criteria.getAmountTypeName(),
				criteria.getRefId(), criteria.getAmount(), criteria.getMemberId(),criteria.getHqId());
	}

	
}
